import httplib2
import json
import urlparse 
import base64
import os


class InetSatError(Exception):
    def __init__(self, value, critical=False):
	self.value = value
	self.critical = critical
    def __str__(self):
	return repr(self.value)


class InetSat(object):
    def __init__(self, username = None, password = None):
	if username is None or password is None:
	    raise
	self.username       = username
	self.password       = password
	self.baseuri        = 'https://admin.inetsat.com'
	self.baseapi  	    = '/api/'
	self.playlistfolder = './'



    def GetHeader(self):
	return { 'Accept': 'application/json',
		 'Authorization' : 'Basic {0}'.format(base64.b64encode('{0}:{1}'.format(self.username, self.password)))
		}

    def VideoServer_GetStatus(self, id=''):
	call   = 'VideoServerApi/GetStatus/?id=' + id
	method = 'Get'
	body   = ''
	header = self.GetHeader()

	target = urlparse.urlparse(self.baseuri+self.baseapi+call)

	h = httplib2.Http()

	response, content = h.request(target.geturl(),method,body,header)
    
	return content

    def Feed_GetFeeds(self):
	call   = 'FeedApi/GetFeeds'
	method = 'GET'
	body   = ''
	header = self.GetHeader()
	
	target = urlparse.urlparse(self.baseuri+self.baseapi+call)

	h = httplib2.Http()

	response, content = h.request(target.geturl(),method,body,header)
    
	return content
    
    def Feed_GetPlaylistItemsStatus(self, feedId='', day=''):
	call   = 'FeedApi/GetPlaylistItemsStatus/?feedId=' + feedId + '&day=' + day 
	method = 'GET'
	body   = ''
	header = self.GetHeader()

	target = urlparse.urlparse(self.baseuri+self.baseapi+call)

	h = httplib2.Http()

	response, content = h.request(target.geturl(),method,body,header)
    
	return json.loads(content)
    
    def Feed_ImportPlaylist(self, feedId='', fileName='', append=False, preview=False,ignoreWarnings=False,ignoreOverwrites=False):

	if feedId == '' or fileName == '':
	    raise InetSatError('Incomplete required argument')

        if os.path.isfile(self.playlistfolder+fileName):
	    f = open(self.playlistfolder+fileName)
	    body = f.read()
	    f.close()
        else:
    	    raise InetSatError('fileName (%s) does not exist in %s' % (fileName,self.playlistfolder))
		
    
	if append:
	    append_c = 'true'
	else:
	    append_c = 'false'

	if preview:
	    preview_c = 'true'
	else:
	    preview_c = 'false'

	call   = 'FeedApi/ImportPlaylist/?feedId=' + feedId + '&fileName=' + fileName + '&append=False' + '&preview=False' + '&ignoreWarnings=True' + '&ignoreOverwrites=True'
	method = 'PUT'
	header = self.GetHeader()
	
	target = urlparse.urlparse(self.baseuri+self.baseapi+call)
	
	h = httplib2.Http()
	
	response, content = h.request(target.geturl(),method,body,header)
	
	return json.loads(content)
	
	

	
	
inet = InetSat('playboylatam@claxson.com','wE5z+Usk')
print inet.VideoServer_GetStatus('9')
print inet.Feed_GetFeeds()
print inet.Feed_ImportPlaylist('147', 'PHSD_2014-10-20-v1(E).osc')

lst = inet.Feed_GetPlaylistItemsStatus('147','2014-10-20')
for i in lst:
    print i


